﻿using AmiDraw.Editor.Factory;
using AmiDraw.Editor.Model;
using AmiDraw.Editor.View;
using AmiDraw.Lib;
using Panuon.UI.Core;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using AmiDraw.Lib.Factory;
using AmiDraw.Lib.Models;
using AmiDraw.Editor.View.Dialog;

namespace AmiDraw.Editor.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class PackEditViewModel
    {
        public static PackEditViewModel instance;

        public DirectoryInfo DirectoryInfo;

        public AcPackInfo PackInfo { get; set; }

        public ObservableCollection<ObservableLevel> ObservableLevels { get; set; }


        public RelayCommand SaveCommand { get; set; }

        public RelayCommand RemoveLevelCommand { get; set; }

        public RelayCommand PackCommand { get; set; }

        public RelayCommand OpenSimulatorCommand { get; set; }

        public RelayCommand OpenMultiplotCommand { get; set; }

        public PackEditViewModel()
        {
            instance = this;
            SaveCommand = new(Save);
            RemoveLevelCommand = new(RemoveLevel);
            PackCommand = new(Pack);
            OpenSimulatorCommand = new(obj => { 
                var dialog = new DrawSimulator();
                dialog.ViewModel.PackInfo = PackInfo;
                dialog.ShowDialog();
            }) ;
            OpenMultiplotCommand = new(obj => {
                var dialog = new MultiplotImageEditView();
                dialog.ViewModel.PackInfo = PackInfo;
                dialog.ShowDialog();
            });
        }


        /// <summary>
        /// 基于目录来获取所有的信息
        /// 目录结构为 info.json collections.json datas文件夹
        /// </summary>
        /// <param name="path"></param>
        public void Load()
        {
            var dir = DirectoryInfo.FullName;

            var infojson = Path.Combine(dir, "info.json");
            var collectionsjson = Path.Combine(dir, "collections.json");


            PackInfo = JsonFactory.LoadJson<AcPackInfo>(infojson);
            ObservableLevels = JsonFactory.LoadJson<ObservableCollection<ObservableLevel>>(collectionsjson);

        }

        /// <summary>
        /// 将所有信息保存。
        /// </summary>
        public void Save(object obj)
        {
            var dir = DirectoryInfo.FullName;

            var infojson = Path.Combine(dir, "info.json");
            var collectionsjson = Path.Combine(dir, "collections.json");

            //保存PackInfo
            JsonFactory.WriteJson(infojson, PackInfo);
            //保存collections.json
            JsonFactory.WriteJson(collectionsjson,ObservableLevels);

            MessageBox.Show("保存完成");

            if (obj != null)
            {
                if (bool.Parse(obj.ToString())) GoHome();
            }

        }


        public void AddLevel()
        {
            ObservableLevels.Add(new()
            {
                Name = "新的等级",
                Ratio = 100,
                Description = "描述",
                Cards = new()
            });
        }

        public void RemoveLevel(object index)
        {
            ObservableLevels.RemoveAt((int)index);
        }

        public void GoHome()
        {
            (App.Current.MainWindow as MainWindow).ViewModel.Content = new HomeView();

        }

        public void Pack(object arg)
        {
            var outputPath = Path.Combine(Directory.GetCurrentDirectory(), "outputs", $"{PackInfo.Name}.acpak");

            FileFactory.InitDir(Path.GetDirectoryName(outputPath));//文件夹不在就创建

            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }


            ZipFile.CreateFromDirectory(DirectoryInfo.FullName,outputPath);

            Process.Start("explorer.exe", Path.GetDirectoryName(outputPath));
        }


    }
}
